/*
 * PreConfig
 * 
 * Version information
 * 
 * Feb 17, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.isso.webapp.struts.action;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.isso.hibernate.Principal;
import net.amsoft.iservice.isso.hibernate.PrincipalMetaData;
import net.amsoft.iservice.isso.service.ISSOService;
import net.amsoft.iservice.isso.util.ISSOConstants;
import net.amsoft.iservice.isso.webapp.dataObject.LoginData;
import net.amsoft.iservice.isso.webapp.dataObject.SPData;
import net.amsoft.iservice.isso.webapp.struts.form.ConfigForm;
import net.amsoft.iservice.util.exception.IServiceException;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.LabelValueBean;

/**
 * This is the action used to take user to the service configuration action
 * 
 * 
 * @author rahul
 * 
 */
public class PreConfig extends BaseAction {
	private static final Logger oLogger = Logger.getLogger(PreConfig.class);

	public ActionForward process(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse)
			throws Exception {
		oLogger.info("process() : entry");
		// get the session data
		LoginData oLoginData = (LoginData) oRequest.getSession(false)
				.getAttribute(ISSOConstants.SESSION_LOGIN_OBJ);
		String sSessionCookie = oLoginData.getSessionCookie();

		if (sSessionCookie == null) {
			ActionMessages oErrors = new ActionErrors();
			oErrors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					ISSOConstants.ERR_INVALID_REQUEST));
			saveErrors(oRequest, oErrors);
			oLogger
					.warn("process() : Message : No Session Cookie is Recieved : Session Cookie: "
							+ sSessionCookie);
			return oMapping.findForward(ISSOConstants.INAME_FWD);
		}

		HashMap ohSPData = (HashMap) oRequest.getSession(false).getAttribute(
				ISSOConstants.SESSION_LOGIN_HASH_OBJ);

		String sSpXri = ISSOConstants.getSPXri(oRequest.getServerName());
		SPData oSPData = (SPData) ohSPData.get(sSpXri);

		if (oSPData == null) {
			ActionMessages oErrors = new ActionErrors();
			oErrors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					ISSOConstants.ERR_INVALID_REQUEST));
			saveErrors(oRequest, oErrors);
			oLogger
					.warn("process() : Message : Service could not be identified, Data : SPData:"
							+ oSPData + ", GIN:" + oSPData.getGlobalInumber());
			return oMapping.findForward(ISSOConstants.INAME_FWD);
		}

		// String sSessionCookie = oLoginData.getSessionCookie();
		String sGlobalInumber = oSPData.getGlobalInumber();
		try {
			ConfigForm oConfigForm = new ConfigForm();
			Principal oPrincipal = ISSOService.getPrincipalProfile(
					sGlobalInumber, sSessionCookie);
			PrincipalMetaData oPrincipalData = ISSOService
					.getMetaData(oPrincipal);

			oConfigForm.setFirstName(oPrincipalData.getFirstName());
			oConfigForm.setLastName(oPrincipalData.getLastName());
			oConfigForm.setEmailAddress(oPrincipalData.getEmail());
			oLogger.debug("process() : FirstName="
					+ oPrincipalData.getFirstName() + ", LastName="
					+ oPrincipalData.getLastName() + ", Email="
					+ oPrincipalData.getEmail());
			oPrincipal = null;
			oPrincipalData = null;

			oPrincipal = ISSOService.getPrincipalSecurityInfo(sGlobalInumber,
					sSessionCookie);

			oPrincipalData = ISSOService.getMetaData(oPrincipal);
			oLogger.info("process() : Message : Fetched Principal Metadata");

			oConfigForm.setGreeting(oPrincipal.getUnEncryptedGreeting());
			oConfigForm.setChallengeQuestion(String.valueOf(oPrincipalData
					.getChallengeQuestionId()));
			oConfigForm.setDupChallengeQuestion(String.valueOf(oPrincipalData
					.getChallengeQuestionId()));
			oConfigForm.setChangePassword(false);

			oRequest.setAttribute(ISSOConstants.REQ_ATTRIB_CONFIG, oConfigForm);

			Hashtable oChallangeQuestions = ISSOConstants
					.getChallangeQuestions();
			// creating arraylist of LabelValueBeans
			ArrayList<LabelValueBean> lQuestions = new ArrayList<LabelValueBean>();
			Enumeration keys = oChallangeQuestions.keys();
			while (keys.hasMoreElements()) {
				String sQno = (String) keys.nextElement();
				LabelValueBean labelValueBean = new LabelValueBean(
						(String) oChallangeQuestions.get(sQno), sQno);
				lQuestions.add(labelValueBean);
			}
			// TODO clean it up after use
			oRequest.getSession(false).setAttribute(
					ISSOConstants.SESSION_CHALLENGE_QUESTIONS, lQuestions);
			oLogger.debug("process() : Message : Attributes set in session");
			oLogger.debug("process() : exit");
			oRequest.setAttribute(ISSOConstants.REQ_PARAM_SPXRI, sSpXri);
			return oMapping.findForward(ISSOConstants.SHOW_CONFIG_FWD);

		} catch (IServiceException e) {
			oLogger.warn("process() : Exception : " + e.getMessage());
			ActionMessages oErrors = new ActionErrors();
			if (e.getErrorCode() == 2035) {
				oErrors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
						ISSOConstants.ERR_SESSION_MISMATCH));
				saveErrors(oRequest.getSession(false), oErrors);
				return oMapping.findForward(ISSOConstants.INAME_FWD);
			} else {
				oErrors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
						ISSOConstants.ERR_INVALID_REQUEST));
				saveErrors(oRequest.getSession(false), oErrors);
			}
			return oMapping.findForward(ISSOConstants.CHKLOGIN_FWD);
		}
	}
}
